package com.digitalchina.lxnsy.viewholder;

import android.app.Activity;
import android.content.Intent;
import androidx.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.ConsultIssueV2Activity;
import com.digitalchina.lxnsy.activity.old.LoginActivity;
import com.digitalchina.lxnsy.entity.MyExpertListVO;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hedgehog.ratingbar.RatingBar;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;

import java.text.MessageFormat;


/**
 * 我的专家列表
 */
public class OperateExpertHolder extends BaseViewHolder<MyExpertListVO> {

    private SimpleDraweeView ivImage;//头像

    private TextView tvExpertName;//姓名
    private TextView tvExpertTitle;//职称
    private TextView tvExpertArea;//涉及领域
    private RatingBar barScore;//评分
    private TextView tvAskNum;  // 提问量
    private TextView tvReplyNum; // 回复量
    private TextView tvStudentNum; // 学员数
    private TextView address; // 地址
    private TextView deleteExpert; // 删除专家
    private LinearLayout consultation; // 沟通
    private DeleteExpertListener listener;
    private Activity activity;

    public OperateExpertHolder(ViewGroup parent, DeleteExpertListener listener, Activity activity) {
        super(parent, R.layout.item_expert_operate);
        this.listener = listener;
        this.activity = activity;
        ivImage = $(R.id.item_iv_expert_iv_avatar);
        tvExpertName = $(R.id.item_expert_list_tv_name);
        consultation = $(R.id.expert_details_consultation);
        deleteExpert = $(R.id.delete_expert);
        tvExpertTitle = $(R.id.item_expert_list_tv_titles);
        tvExpertArea = $(R.id.item_expert_list_tv_area);
        barScore = $(R.id.item_expert_list_bar_score);

        tvAskNum = $(R.id.item_expert_list_tv_ask_num);
        tvReplyNum = $(R.id.item_expert_list_tv_reply_num);
        tvStudentNum = $(R.id.item_expert_list_tv_student_num);
        address = $(R.id.address);
    }

    @Override
    public void setData(final MyExpertListVO data) {
        super.setData(data);

        tvAskNum.setText(MessageFormat.format("提问量：{0}", StringUtil.isStrEmpty((String)data.getExpertInfo().getQuestionNUm()) ? "0" : (String)data.getExpertInfo().getQuestionNUm()));
        tvReplyNum.setText(MessageFormat.format("回复量：{0}", StringUtil.isStrEmpty((String)data.getExpertInfo().getAnswerNum()) ? "0" : (String)data.getExpertInfo().getAnswerNum()));
        tvStudentNum.setText(MessageFormat.format("学员数：{0}", StringUtil.isStrEmpty((String)data.getExpertInfo().getStudentNum()) ? "0" : (String)data.getExpertInfo().getStudentNum()));
        if(null != data.getExpertInfo())
        address.setText((data.getExpertInfo().getProvince() == null?"":data.getExpertInfo().getProvince().getName()) + (data.getExpertInfo().getCity() == null?"":data.getExpertInfo().getCity().getName())
                + (data.getExpertInfo().getCounty() == null?"":data.getExpertInfo().getCounty().getName()));

        FrescoUtil.showImageSmall(data.getExpertInfo().getPhoto() != null ? data.getExpertInfo().getPhoto() : "", ivImage);

        tvExpertName.setText(!StringUtil.isStrEmpty(data.getExpertInfo().getExpertName()) ? data.getExpertInfo().getExpertName() : "匿名专家");

        consultation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!Utils.isLogin(activity)) {
                    activity.startActivityForResult(new Intent(activity, LoginActivity.class), 1);
                    return;
                }

                if (data == null)
                    return;
                GotoUtil.gotoActivity(activity, ConsultIssueV2Activity.class, "single", data.getExpertInfo());
            }
        });

        deleteExpert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MaterialDialog.Builder(getContext())
                        .title("提示")
                        .content("确定要删除此专家？")
                        .positiveText("删除")
                        .negativeText("取消")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                // do something
                                listener.deletClick(getAdapterPosition());
                            }
                        })
                        .show();

            }
        });

//        if (!TextUtils.isEmpty(data.getExpertSex())) {
//            if (data.getExpertSex().equals("男")) {
//                isMale();
//            } else {
//                isFemale();
//            }
//        }
        if (data.getExpertInfo().getExpertTitle() == null || StringUtil.isStrEmpty(data.getExpertInfo().getExpertTitle().getTitle())) {
            tvExpertTitle.setVisibility(View.GONE);
        } else {
            tvExpertTitle.setVisibility(View.VISIBLE);
            tvExpertTitle.setText(data.getExpertInfo().getExpertTitle().getTitle());
        }
//        tvExpertArea.setText(data.getExpertInfo().getExpertClassification() != null ? data.getExpertInfo().getExpertClassification().getPname() + "  " + data.getExpertInfo().getExpertClassification().getName() : "");
        if (data.getExpertInfo().getExpertAreas() != null)
        {
            String s = "";
            for (int i = 0; i < data.getExpertInfo().getExpertAreas().size(); i++)
            {
                s += data.getExpertInfo().getExpertAreas().get(i).getExpertAreaType().getName() + "-"
                        + data.getExpertInfo().getExpertAreas().get(i).getExpertAreaDirection().getName() + "  ";
            }
            tvExpertArea.setText(s);
        }
        barScore.setStar((float) data.getExpertInfo().getScore());


    }

    public interface DeleteExpertListener{
        void deletClick(int pos);
    }
}
